package com.jgp.form.controller;

import com.jgp.common.annotation.UIParam;
import com.jgp.form.model.VirtualForm;
import com.jgp.form.model.VirtualFormAttr;
import com.jgp.form.props.FormPropBean;
import com.jgp.form.service.FormService;
import com.jgp.form.vo.*;
import com.jgp.mico.client.MicroClient;
import com.jgp.mico.client.UriVariableStyle;
import com.jgp.sys.controller.JGPBackController;
import com.jgp.sys.ui.GridResult;
import com.jgp.sys.ui.Pager;
import com.jgp.sys.ui.Result;
import com.jgp.sys.utils.SpringContextUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 项目   jgp-cloud-parent
 * 作者   loufei
 * 时间   2019-09-02
 */
@CrossOrigin
@RestController
@RequestMapping("form/formApiController")
public class FormApiController extends JGPBackController {
    
    @Autowired
    private FormPropBean propBean;
    
    @Autowired
    private FormService formService;
    
    @RequestMapping("saveForm")
    public Result saveForm(VirtualForm form){
        if(ifInvokeFromRemote(propBean)){
            MicroClient microClient = SpringContextUtil.getBean(MicroClient.class);
            return microClient.post(propBean.getMicroAppName(),"/form/formApiController/saveForm",Result.class,form);
        }
        formService.saveForm(form);
        return ajaxRe(true);
    }
    
    @RequestMapping("queryForm")
    public Result queryForm(String name,@UIParam("pager")Pager pager){
        if(ifInvokeFromRemote(propBean)){
            MicroClient microClient = SpringContextUtil.getBean(MicroClient.class);
            LinkedHashMap<String,Object> uriVariables = new LinkedHashMap<>();
            uriVariables.put("name",name);
            return microClient.post(propBean.getMicroAppName(), "/form/formApiController/queryForm", Result.class, UriVariableStyle.KEY_VALUE_STYLE,uriVariables,pager);
        }
        
        List<VirtualForm> list = formService.queryForm(name,pager);
        List<GridResult.Category> categories = new ArrayList<>();
        categories.add(new GridResult.Category("表单名","name"));
        categories.add(new GridResult.Category("编码","code"));
        //categories.add(new GridResult.Category("备注","remark"));
        return ajaxReGrid("gdata",categories,list,pager);
    }
    
    @RequestMapping("queryFormById/{formId}")
    public Result queryFormById(@PathVariable String formId){
        
        if(ifInvokeFromRemote(propBean)){
            MicroClient microClient = SpringContextUtil.getBean(MicroClient.class);
            LinkedHashMap<String,Object> uriVariables = new LinkedHashMap<>();
            uriVariables.put("formId",formId);
            return microClient.post(propBean.getMicroAppName(),"/form/formApiController/queryFormById",Result.class,UriVariableStyle.PATH_STYLE,uriVariables);
        }
        
        return ajaxReForm("fdata",formService.queryForm(formId));
    }
    
    @RequestMapping("removeForm/{formId}")
    public Result removeForm(@PathVariable String formId){
        if(ifInvokeFromRemote(propBean)){
            MicroClient microClient = SpringContextUtil.getBean(MicroClient.class);
            LinkedHashMap<String,Object> uriVariables = new LinkedHashMap<>();
            uriVariables.put("formId",formId);
            return microClient.post(propBean.getMicroAppName(),"/form/formApiController/removeForm",Result.class,UriVariableStyle.PATH_STYLE,uriVariables);
        }
        formService.removeForm(formId);
        return ajaxRe(true);
    }
    
    @RequestMapping("formInfo")
    public Result formInfo(String formId,String formCode){
        if(ifInvokeFromRemote(propBean)){
            MicroClient microClient = SpringContextUtil.getBean(MicroClient.class);
            LinkedHashMap<String,Object> uriVariables = new LinkedHashMap<>();
            if(StringUtils.isNotBlank(formId)){
                uriVariables.put("formId",formId);
            }
            if(StringUtils.isNotBlank(formCode)){
                uriVariables.put("formCode",formCode);
            }
            return microClient.post(propBean.getMicroAppName(),"/form/formApiController/formInfo",Result.class,UriVariableStyle.KEY_VALUE_STYLE,uriVariables);
        }
        String key = StringUtils.isNotBlank(formId)?formId:formCode;
        return ajaxReData("formInfo",formService.formInfo(key));
    }
    @RequestMapping("virtualForm")
    public Result virtualForm(String key){
        if(ifInvokeFromRemote(propBean)){
            MicroClient microClient = SpringContextUtil.getBean(MicroClient.class);
            LinkedHashMap<String,Object> uriVariables = new LinkedHashMap<>();
            if(StringUtils.isNotBlank(key)){
                uriVariables.put("key",key);
            }
            return microClient.post(propBean.getMicroAppName(),"/form/formApiController/virtualForm",Result.class,UriVariableStyle.KEY_VALUE_STYLE,uriVariables);
        }
        return ajaxReData("formInfo",formService.virtualForm(key));
    }
    @RequestMapping("saveFormAttr")
    public Result saveFormAttr(VirtualFormAttr formAttr){
        if(ifInvokeFromRemote(propBean)){
            MicroClient microClient = SpringContextUtil.getBean(MicroClient.class);
            return microClient.post(propBean.getMicroAppName(),"/form/formApiController/saveFormAttr",Result.class,formAttr);
        }
        
        formService.saveFormAttr(formAttr);
        return ajaxRe(true);
    }
    
    
    
    @RequestMapping("removeFormAttr/{attrId}")
    public Result removeFormAttr(@PathVariable String attrId){
        if(ifInvokeFromRemote(propBean)){
            MicroClient microClient = SpringContextUtil.getBean(MicroClient.class);
            LinkedHashMap<String,Object> uriVariables = new LinkedHashMap<>();
            uriVariables.put("attrId",attrId);
            return microClient.post(propBean.getMicroAppName(),"/form/formApiController/removeFormAttr",Result.class,UriVariableStyle.PATH_STYLE,uriVariables);
        }
        formService.removeFormAttr(attrId);
        return ajaxRe(true);
    }
    
    
    @RequestMapping("queryFormAttr/{formId}")
    public Result queryFormAttr(String label,@PathVariable String formId, @UIParam("pager")Pager pager){
    
        if(ifInvokeFromRemote(propBean)){
            MicroClient microClient = SpringContextUtil.getBean(MicroClient.class);
            LinkedHashMap<String,Object> uriVariables = new LinkedHashMap<>();
            uriVariables.put("formId",formId);
            Map<String,String> params = new HashMap();
            params.put("label",label);
            return microClient.post(propBean.getMicroAppName(),"/form/formApiController/queryFormAttr",Result.class,UriVariableStyle.PATH_STYLE,uriVariables,params,pager);
        }
        
        
        List<VirtualFormAttr> list = formService.queryFormAttr(label,formId,pager);
        List<GridResult.Category> categories = new ArrayList<>();
        categories.add(new GridResult.Category("LABEL","label"));
        categories.add(new GridResult.Category("NAME","name"));
        categories.add(new GridResult.Category("类型","type"));
        //categories.add(new GridResult.Category("描述","remark"));
        return ajaxReGrid("gdata",categories,list,pager);
    }
    
    
    @RequestMapping("queryFormAttrById/{attrId}")
    public Result queryFormAttrById(@PathVariable String attrId){
        if(ifInvokeFromRemote(propBean)){
            MicroClient microClient = SpringContextUtil.getBean(MicroClient.class);
            LinkedHashMap<String,Object> uriVariables = new LinkedHashMap<>();
            uriVariables.put("attrId",attrId);
            return microClient.post(propBean.getMicroAppName(),"/form/formApiController/queryFormAttrById",Result.class,UriVariableStyle.PATH_STYLE,uriVariables);
        }
        return ajaxReForm("fdata",formService.queryFormAttrById(attrId));
    }
    
    @RequestMapping("queryFormAttrForSort/{formId}")
    public Result queryFormAttrForSort(@PathVariable String formId, @UIParam("pager")Pager pager){
        if(ifInvokeFromRemote(propBean)){
            MicroClient microClient = SpringContextUtil.getBean(MicroClient.class);
            LinkedHashMap<String,Object> uriVariables = new LinkedHashMap<>();
            uriVariables.put("formId",formId);
            return microClient.post(propBean.getMicroAppName(),"/form/formApiController/queryFormAttrForSort",Result.class,UriVariableStyle.PATH_STYLE,uriVariables,pager);
        }
        
        List<VirtualFormAttr> list = formService.queryFormAttr(null,formId,pager);
        List<GridResult.Category> categories = new ArrayList<>();
        categories.add(new GridResult.Category("LABEL","label"));
        categories.add(new GridResult.Category("排序","sort"));
        return ajaxReGrid("gdata",categories,list,pager);
    }
    
    @RequestMapping("configFormAttr")
    public Result configFormAttr(@RequestBody VirtualFormAttrConfig config){
        if(ifInvokeFromRemote(propBean)){
            MicroClient microClient = SpringContextUtil.getBean(MicroClient.class);
            return microClient.postJson(propBean.getMicroAppName(),"/form/formApiController/configFormAttr",Result.class,config);
        }
        formService.configFormAttr(config);
        return ajaxRe(true);
    }
    
    @RequestMapping("saveFormValue")
    public Result saveFormValue(@RequestBody VirtualFormData formData){
        if(ifInvokeFromRemote(propBean)){
            MicroClient microClient = SpringContextUtil.getBean(MicroClient.class);
            return microClient.postJson(propBean.getMicroAppName(),"/form/formApiController/saveFormValue",Result.class,formData);
        }
        formService.createFormValue(formData);
        return ajaxRe(true);
    }
    
    @RequestMapping("removeFormValue/{bizId}")
    public Result removeFormValue(@PathVariable String bizId){
        if(ifInvokeFromRemote(propBean)){
            MicroClient microClient = SpringContextUtil.getBean(MicroClient.class);
            LinkedHashMap<String,Object> uriVariables = new LinkedHashMap<>();
            uriVariables.put("bizId",bizId);
            return microClient.post(propBean.getMicroAppName(),"/form/formApiController/removeFormValue",Result.class,UriVariableStyle.PATH_STYLE,uriVariables);
        }
        formService.removeFormValue(bizId);
        return ajaxRe(true);
    }
    
    @RequestMapping("queryFormValue")
    public Result queryFormValue(@RequestBody VirtualFormValueQuery query){
        if(ifInvokeFromRemote(propBean)){
            MicroClient microClient = SpringContextUtil.getBean(MicroClient.class);
            return microClient.postJson(propBean.getMicroAppName(),"/form/formApiController/queryFormValue",Result.class,query);
        }
        
        //处理查询条件
        /*QueryFilterList extFilters = query.getFilters().extFilters("location");
        /*QueryFilterList extFilters = query.getFilters().extFilters("xxx");
        extFilters.forEach(filter -> {
            if(filter.getName().equals("bridgeName")){
                filter.setOperator(Operator.like);
            }
        });
        
        // 关联其他表
        Join join = new Join("bizId", TestModel.class, JoinDirection.leftJoin,"id");
        join.setJoinFilters(extFilters);
        OrderList orders = new OrderList();
        orders.addOrder("bridgeName",OrderDirection.ASC);
        join.setJoinOrders(orders);
        
        
        VirtualFormResponse response = formService.queryFormValue(query,join);
        */
        VirtualFormResponse response = formService.queryFormValue(query,null);
        //添加扩展列
        List<GridResult.Category> categories = response.getCategories();
        return ajaxReGrid("gdata",categories,response.getList(),response.getPager());
    }
    
    @RequestMapping("formValue")
    public Result formValue(String bizId){
        if(ifInvokeFromRemote(propBean)){
            MicroClient microClient = SpringContextUtil.getBean(MicroClient.class);
            LinkedHashMap<String,Object> uriVariables = new LinkedHashMap<>();
            uriVariables.put("bizId",bizId);
            return microClient.post(propBean.getMicroAppName(),"/form/formApiController/formValue",Result.class,UriVariableStyle.KEY_VALUE_STYLE,uriVariables);
        }
        VirtualFormResponseData response = formService.formValue(bizId);
        return ajaxReForm("fdata",response);
    }
}
